<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image class="logo" src="/static/logo.png"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				//logo图片 base64
				phoneData:'', //用户
				passData:'', //密码
				basePass:'', //加密的密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			//this.isLogin();
		},
		methods: {
			isLogin(){
				
			},
		    startLogin(e){
                
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 6) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				
				console.log("登录成功")
				
				_this.isRotate=true
				
				var b = new Buffer(this.passData);
				this.basePass = b.toString('base64');
				console.log(this.basePass); 
				
				uni.request({
				    url: this.$httpUrl +'/admin/login/login', //仅为示例，并非真实接口地址。
				    method: 'POST',
					data: {
				        username: this.phoneData,
						password: this.basePass
				    },
				    success: (res) => {
				        console.log(res.data.data.token);
						if(res.data.code == 0){
							console.log(res.data.data.userMess)
							//存储登陆对象
							var person = {username:this.phoneData, password: this.basePass}
							
							uni.setStorageSync('token', res.data.data.userMess.token);
							uni.setStorageSync('userMess', res.data.data.userMess);
							uni.setStorageSync('loginObjet', person);
							
							_this.isRotate=false
							
							uni.reLaunch({
							    url: '/pages/index/index'
							});
						} else {
							this.$uniApi.open(res.data.msg)
							_this.isRotate=false
						}
				        
				    },
					fail: (res) => { //异常
						this.$uniApi.open('登陆失败！')
						_this.isRotate=false
					}
				});
		    }
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
